from flask import Flask, request, jsonify, render_template
import subprocess

app = Flask(__name__)

@app.route('/')
def index():
        return render_template('index.html')

@app.route('/generate', methods=['GET'])
def generate():
    print("in generate")
    number_cha = 512
    prompt = request.args.get('prompt', default=1, type=int)
    # prompt = request.json['prompt']
    command = [
        "./llama-cli",
        "-m", "/mnt/hgfs/ccc/qwen2-1_5b-instruct-q3_k_m.gguf",
        "-p", prompt, 
        "-n", str(number_cha),
    ]
    print(command)
    result = subprocess.run(command, capture_output=True, text=True)
    print("finish result")
    return jsonify({"response": result.stdout})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)